home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip0493.zip / PUSHDIR.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  5KB  |  192 lines

  1. /*
  2. **  PushDir() and PopDir()
  3. **
  4. **  Original Copyright 1988-1991 by Bob Stout as part of
  5. **  the MicroFirm Function Library (MFL)
  6. **
  7. **  This subset version is an expanded version of the one
  8. **  originally published by the author in Tech Specialist
  9. **  magazine and is hereby donated to the public domain.
  10. */
  11.  
  12. #include <dos.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15.  
  16. #ifdef __TURBOC__
  17.  #include <dir.h>
  18. #else
  19.  #include <direct.h>
  20. #endif
  21.  
  22. #define DIR_STACK_SIZE  8
  23. #define MAX_FLEN        67
  24.  
  25. typedef enum {ERROR = -1, FALSE, TRUE} LOGICAL;
  26.  
  27. #define BOOL(x) (!(!(x)))
  28.  
  29. /*
  30. **  NOTE: Uses the author's chdrv(), also in SNIPPETS!
  31. */
  32.  
  33. int chdrv(int);
  34.  
  35. static int  PushDir_stack_ptr;
  36. static char PushDir_stack[DIR_STACK_SIZE][MAX_FLEN];
  37.  
  38. /*
  39. **  PushDir()
  40. **
  41. **  Like chdir(), except a drive may be specified and the old directory
  42. **  is saved.
  43. **
  44. **  Arguments: 1 - newdir, the buffer containing the new directory name
  45. **
  46. **  Returns:  -1 - stack overflow
  47. **             0 - error
  48. **             1 - success, still on same drive
  49. **             2 - success, changed drive
  50. **
  51. **  Side effects: Converts name in newdir to upper case and prepends
  52. **                a drive letter.
  53. **
  54. **  CAUTION: Since a drive will be prepended to newdir, it's buffer
  55. **           should be at at least MAX_FLEN long.
  56. */
  57.  
  58. int PushDir(char *newdir)
  59. {
  60.       char pname[MAX_FLEN];
  61.       char drive[3];
  62.       char *target = &pname[2];
  63.       int i, new_drv = 0, ercode = 0;
  64.       static int init = 0;
  65.  
  66.       if (!init)
  67.             PushDir_stack_ptr = init = -1;
  68.       if (DIR_STACK_SIZE <= ++PushDir_stack_ptr)
  69.       {
  70.             ercode = -1;
  71.             goto ErrEx;
  72.       }
  73.       getcwd(PushDir_stack[PushDir_stack_ptr], MAX_FLEN);
  74.       strupr(PushDir_stack[PushDir_stack_ptr]);
  75.       strncpy(drive, PushDir_stack[PushDir_stack_ptr], 2);
  76.       drive[2] = '\0';
  77.       if (':' == newdir[1])
  78.       {     /* If a drive is specified                                  */
  79.             strupr(newdir);
  80.             strcpy(pname, newdir);
  81.             if (strchr(target, ':'))      /* if filename is illegal     */
  82.                   goto ErrEx;
  83.             if (*drive != *newdir)
  84.             {
  85.                   if (ERROR == chdrv(newdir[0]))
  86.                   {     /* If the drive is invalid                      */
  87.                         goto ErrEx;
  88.                   }
  89.                   else  new_drv = 1;
  90.             }
  91.       }
  92.       else
  93.       {     /* If a drive isn't specified                               */
  94.             if (!strchr(strupr(newdir), ':'))
  95.             {     /* If legal filename                                  */
  96.                   strcpy(pname, drive);
  97.                   strcat(pname, newdir);
  98.                   strcpy(newdir, pname);
  99.             }
  100.             else
  101.             {     /* If filename is illegal                             */
  102.                   goto ErrEx;
  103.             }
  104.       }
  105.  
  106.       if (*target)
  107.       {
  108.             if (chdir(target))
  109.             {
  110.                   if (1 == new_drv) /* We already changed drives        */
  111.                         chdrv(*drive);    /* Go home before exit        */
  112.                   goto ErrEx;
  113.             }
  114.       }
  115.       return (new_drv + 1);
  116. ErrEx:
  117.       --PushDir_stack_ptr;
  118.       return (ercode);
  119. }
  120.  
  121. /*
  122. **  PopDir()
  123. **
  124. **  Like chdir(), except goes to the drive/directory specified on the
  125. **  top of the PushDir stack.
  126. **
  127. **  Arguments: none
  128. **
  129. **  Returns:  -1 - stack empty
  130. **             0 - error - stack pointer unchanged
  131. **             1 - success, still on same drive
  132. **             2 - success, changed drive
  133. **
  134. **  Side effects: none
  135. **
  136. **  CAUTION: chdir() or chdrv() should not be called between PushDir-
  137. **           PopDir calls.
  138. */
  139.  
  140. int PopDir(void)
  141. {
  142.       char I_am_here[MAX_FLEN], target_drv, *target;
  143.       int new_drv = 0;
  144.  
  145.       if (0 > PushDir_stack_ptr)
  146.             return -1;
  147.       getcwd(I_am_here, MAX_FLEN);
  148.       target = &PushDir_stack[PushDir_stack_ptr][2];
  149.       target_drv = PushDir_stack[PushDir_stack_ptr][0];
  150.       if (I_am_here[0] != target_drv)
  151.       {
  152.             if (ERROR == chdrv(target_drv))
  153.                   return 0;
  154.             new_drv = 1;
  155.       }
  156.       if (!chdir(target))
  157.       {
  158.             --PushDir_stack_ptr;
  159.             return (1 + new_drv);
  160.       }
  161.       else  return 0;
  162. }
  163.  
  164. /*
  165. **  isdir()
  166. **
  167. **  Checks to see if a drive and/or path are a valid directory.
  168. **
  169. **  Arguments: 1 - dir, the buffer containing the new directory name
  170. **
  171. **  Returns: ERROR - push/popdir stack overflow
  172. **           FALSE - not a valid directory
  173. **           TRUE  - valid directory
  174. **
  175. **  Side effects: Converts name in dir to upper case and prepends a
  176. **                drive letter.
  177. **
  178. **  CAUTION: Since a drive will be prepended to newdir, it's buffer
  179. **           should be at at least MAX_FLEN long.
  180. */
  181.  
  182. int isdir(char *dir)
  183. {
  184.       int ercode;
  185.  
  186.       if (-1 == (ercode = PushDir(dir)))
  187.             return ercode;
  188.       if (ercode)
  189.             PopDir();
  190.       return BOOL(ercode);
  191. }
  192.